package com.lzy.cloud.base.web;

import com.lzy.cloud.base.entity.InterfaceLimit;
import com.lzy.cloud.base.service.InterfaceLimitService;
import com.lzy.framework.core.output.Result;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicLong;

@RestController
public class ComputeController {

    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private InterfaceLimitService service;

    //用来标记调用次数
    private static AtomicLong num = new AtomicLong(0);

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public Result add(@RequestParam Integer a, @RequestParam Integer b) {
        InterfaceLimit limit = service.getEntityByPri(1);
        ServiceInstance instance = client.getLocalServiceInstance();
        Integer r = a + b;
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return new Result("From " + instance.getServiceId() + ", Result is " + r + " Port:" + instance.getPort());
    }

}